﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CostaRicaPureTravelUtils.PRResourcesUtils;
using CostaRicaPureTravel.Controller;
using CostaRicaPureTravel.UserControls;
using System.Text;
using CostaRicaPureTravelUtils.EmailConfiguration;

namespace CostaRicaPureTravel
{
    public partial class CustomizedTrips : System.Web.UI.Page
    {
        private EmailController controller;

        protected void Page_Init(object sender, EventArgs e)
        {
            controller = new EmailController(this.Context);
            AjaxPopUp.EventDelegate = btnOkEvent;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    String name = String.Format("{0} {1}", txtFirstName.Text, txtLastName.Text);
                    String body = EmailContent.getEmailTemplate(EmailContent.EmailType.CUSTOMIZE_TRIPS, name, txtPhone.Text, dropWhoAreYou.SelectedItem.Text, dropAbleArrage.SelectedItem.Text, txtMessage.Text, createBody());
                    controller.Send(txtEmailAddress.Text, name, EmailContent.CUSTOMIZE_TRIP, body, true);
                }
                else {
                    return;
                }
            }
            catch (Exception)
            {
                AjaxPopUp.MessageType = AjaxBasedPopUpControl.PopupMessageType.Error;
                AjaxPopUp.Message = "CONTACT_GENERAL_ERROR";
            }
            AjaxPopUp.ShowPopupMessage();
        }

        private string createBody()
        {
            List<ListItem> selected = trips.Items.Cast<ListItem>()
                .Where(li => li.Selected)
                .ToList();

            StringBuilder body = new StringBuilder();
            for (int i = 0; i < selected.Count; i++ )
            {
                body.Append(selected[i].Text);
                if (i < selected.Count -1)
                    body.Append(", ");
            }

            return body.ToString();
        }

        protected void btnOkEvent(object sende, EventArgs e)
        {
            Response.Redirect(ResolveUrl("CustomizedTrips.aspx"));
        }
    }
}